package com.sgb.kjwl.viewmodel.manage;

import android.app.Application;
import android.content.Context;
import android.text.TextUtils;

import androidx.annotation.NonNull;
import androidx.lifecycle.AndroidViewModel;
import androidx.lifecycle.MutableLiveData;

import com.google.gson.reflect.TypeToken;
import com.sgb.kjwl.api.APIManager;
import com.sgb.link.entity.PickFilterEntity;
import com.sgb.kjwl.model.entity.TwoLevelChildForPopWindowEntity;
import com.sgb.kjwl.model.entity.TwoLevelForPopWindowEntity;
import com.sgb.kjwl.model.entity.manage.TheEnterpriseOfEntity;
import com.sgb.kjwl.model.entity.self.PermissionMenuEntity;
import com.sgb.kjwl.model.entity.transport.MapLongAndLatEntity;
import com.sgb.kjwl.model.entity.user.AddressEntity;
import com.swgk.core.base.model.entity.BaseEntity;
import com.swgk.core.base.model.entity.ListResponse;
import com.swgk.core.dialog.DialogHelper;
import com.swgk.core.mmkvutil.CacheMarker;
import com.swgk.core.mmkvutil.DatalUtils;
import com.swgk.core.mmkvutil.MkvUtils;
import com.swgk.core.util.MToast;
import com.swgk.core.util.PickSelectEntity;
import com.swgk.core.util.SharedPreferenceUtil;
import org.jetbrains.annotations.NotNull;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class EnterpriseAndMapModel2 extends AndroidViewModel {

    private List<PickSelectEntity> list;
    public MutableLiveData<List<PickSelectEntity>> mCapital = new MutableLiveData<>();
    public MutableLiveData<List<PickSelectEntity>> mDistance = new MutableLiveData<>();
    public MutableLiveData<List<PickSelectEntity>> mType = new MutableLiveData<>();
    public MutableLiveData<List<AddressEntity<List<AddressEntity<List<AddressEntity>>>>>> mCity = new MutableLiveData<>();
    public MutableLiveData<List<TwoLevelChildForPopWindowEntity<List<TwoLevelForPopWindowEntity>>>> mTradeType = new MutableLiveData<>();
    public MutableLiveData<Boolean> mFollow = new MutableLiveData<>();
    public MutableLiveData<List<TheEnterpriseOfEntity>> mCom = new MutableLiveData<>();
    public MutableLiveData<List<PickSelectEntity<List<PickSelectEntity>>>> mTypeFirst = new MutableLiveData<>();
    public MutableLiveData<MapLongAndLatEntity> locationLiveData = new MutableLiveData<>();

    public List<PermissionMenuEntity> permission_list = new ArrayList<>();
    public MutableLiveData<ArrayList<PickFilterEntity<Object>>> mCheckMenusLiveData = new MutableLiveData<>();
    private ArrayList<PickFilterEntity<Object>> mCheckMenus = new ArrayList<>();

    public EnterpriseAndMapModel2(@NonNull @NotNull Application application) {
        super(application);
    }

    //获取注册资金
    public void findCapitalList(Context context) {
        DialogHelper.showProgressDialog(context, null, "数据加载中...", 0, false, null).setCanceledOnTouchOutside(false);
        APIManager.getInstance().getManageAPI().getPersonNumber().enqueue(new Callback<BaseEntity<List<PickSelectEntity>>>() {
            @Override
            public void onResponse(Call<BaseEntity<List<PickSelectEntity>>> call, Response<BaseEntity<List<PickSelectEntity>>> response) {
                DialogHelper.dismissProgressDialog();
                if (response.body() == null || response.body().getState() == null) {
                    return;
                }
                if (response.body().getState().equals(APIManager.RESPONSE_OK) && response.body().getData() != null) {
                    List<PickSelectEntity> list = response.body().getData();
                    mCapital.postValue(list);
                } else {
                    MToast.showLongToast(context, response.body().getMsg());
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<List<PickSelectEntity>>> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
            }
        });
    }

    //获取经纬度信息
    public void getLocationLL(Context context, String provinceNo, String cityNo, String districtNo) {
        DialogHelper.showProgressDialog(context, "加载中...").setCanceledOnTouchOutside(false);
        APIManager.getInstance().getDriverAPI().getLocationLL("", provinceNo, cityNo, districtNo).enqueue(new Callback<BaseEntity<MapLongAndLatEntity>>() {
            @Override
            public void onResponse(Call<BaseEntity<MapLongAndLatEntity>> call, Response<BaseEntity<MapLongAndLatEntity>> response) {
                DialogHelper.dismissProgressDialog();
                if (response == null || response.body() == null) return;
                if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
                    locationLiveData.postValue(response.body().getData());
                } else {
                    MToast.showToast(context, response.message());
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<MapLongAndLatEntity>> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
                MToast.showToast(context, t.getMessage());
            }
        });

    }

    //获取距离
    public void getDistanceDatas() {
        if (list == null) {
            list = new ArrayList<>();
            PickSelectEntity entity1 = new PickSelectEntity("3", "默认");
            list.add(entity1);
            PickSelectEntity entity;
            int m;
            for (int i = 0; i < 7; i++) {
                m = 10 + 10 * i;
                entity = new PickSelectEntity(m + "", m + "KM");
                list.add(entity);
            }
        }
        mDistance.postValue(list);
    }

    //获取列表数据
    public void getComList(Map<String, Object> map, Context context) {
        DialogHelper.showProgressDialog(context, null, "数据加载中...", 0, false, null).setCanceledOnTouchOutside(false);
        APIManager.getInstance().getManageAPI().getComList(map).enqueue(new Callback<BaseEntity<ListResponse<List<TheEnterpriseOfEntity>>>>() {
            @Override
            public void onResponse(Call<BaseEntity<ListResponse<List<TheEnterpriseOfEntity>>>> call, Response<BaseEntity<ListResponse<List<TheEnterpriseOfEntity>>>> response) {
                DialogHelper.dismissProgressDialog();
                if (response.body() == null || response.body().getState() == null) {
                    return;
                }
                if (response.body().getState().equals(APIManager.RESPONSE_OK) && response.body().getData() != null) {
                    mCom.postValue(response.body().getData().getList());
                } else {
                    MToast.showLongToast(context, "数据异常");
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<ListResponse<List<TheEnterpriseOfEntity>>>> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
            }
        });
    }

    public void getType(Context context) {
        DialogHelper.showProgressDialog(context, null, "数据加载中...", 0, false, null).setCanceledOnTouchOutside(false);
        APIManager.getInstance().getManageAPI().getType().enqueue(new Callback<BaseEntity<List<PickSelectEntity>>>() {
            @Override
            public void onResponse(Call<BaseEntity<List<PickSelectEntity>>> call, Response<BaseEntity<List<PickSelectEntity>>> response) {
                DialogHelper.dismissProgressDialog();
                if (response.body() == null || response.body().getState() == null) {
                    return;
                }
                if (response.body().getState().equals(APIManager.RESPONSE_OK) && response.body().getData() != null) {
                    mType.postValue(response.body().getData());
                } else {
                    MToast.showLongToast(context, response.body().getMsg());
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<List<PickSelectEntity>>> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
            }
        });
    }

    public void getCityData(Context context) {
        DialogHelper.showProgressDialog(context, null, "数据加载中...", 0, false, null).setCanceledOnTouchOutside(false);
        APIManager.getInstance().getGoodsAPI().getCityList().enqueue(new Callback<BaseEntity<List<AddressEntity<List<AddressEntity<List<AddressEntity>>>>>>>() {
            @Override
            public void onResponse(Call<BaseEntity<List<AddressEntity<List<AddressEntity<List<AddressEntity>>>>>>> call, Response<BaseEntity<List<AddressEntity<List<AddressEntity<List<AddressEntity>>>>>>> response) {
                DialogHelper.dismissProgressDialog();
                if (response.body() == null || response.body().getData() == null) {
                    return;
                }
                if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
                    mCity.postValue(response.body().getData());
                } else {
                    MToast.showToast(context, response.body().getMsg());
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<List<AddressEntity<List<AddressEntity<List<AddressEntity>>>>>>> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
            }
        });
    }

    public void getTradeTypes(Context context) {
        DialogHelper.showProgressDialog(context, null, "数据加载中...", 0, false, null).setCanceledOnTouchOutside(false);
        APIManager.getInstance().getManageAPI().getCompanyTradeType().enqueue(new Callback<BaseEntity<List<TwoLevelChildForPopWindowEntity<List<TwoLevelForPopWindowEntity>>>>>() {
            @Override
            public void onResponse(Call<BaseEntity<List<TwoLevelChildForPopWindowEntity<List<TwoLevelForPopWindowEntity>>>>> call, Response<BaseEntity<List<TwoLevelChildForPopWindowEntity<List<TwoLevelForPopWindowEntity>>>>> response) {
                DialogHelper.dismissProgressDialog();
                if (response.body() == null || response.body().getData() == null) {
                    return;
                }
                if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
                    mTradeType.postValue(response.body().getData());
                } else {
                    MToast.showToast(context, response.body().getMsg());
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<List<TwoLevelChildForPopWindowEntity<List<TwoLevelForPopWindowEntity>>>>> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
            }
        });
    }

    public void follow(String shopId, Context context) {
        DialogHelper.showProgressDialog(context, null, "数据加载中...", 0, false, null).setCanceledOnTouchOutside(false);
        APIManager.getInstance().getGoodsAPI().userFocusCompany(shopId).enqueue(new Callback<BaseEntity>() {
            @Override
            public void onResponse(Call<BaseEntity> call, Response<BaseEntity> response) {
                DialogHelper.dismissProgressDialog();
                if (response.body() == null || response.body().getData() == null) {
                    return;
                }
                if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
                    mFollow.postValue(true);
                } else {
                    MToast.showToast(context, response.body().getMsg());
                }
            }

            @Override
            public void onFailure(Call<BaseEntity> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
            }
        });
    }

    public void getTypeList(Context context) {
        DialogHelper.showProgressDialog(context, null, "数据加载中...", 0, false, null).setCanceledOnTouchOutside(false);
        APIManager.getInstance().getManageAPI().getTypeList().enqueue(new Callback<BaseEntity<List<PickSelectEntity<List<PickSelectEntity>>>>>() {
            @Override
            public void onResponse(Call<BaseEntity<List<PickSelectEntity<List<PickSelectEntity>>>>> call, Response<BaseEntity<List<PickSelectEntity<List<PickSelectEntity>>>>> response) {
                DialogHelper.dismissProgressDialog();
                if (response.body() == null || response.body().getState() == null) {
                    return;
                }
                if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
                    mTypeFirst.postValue(response.body().getData());
                } else {
                    MToast.showLongToast(context, response.body().getMsg());
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<List<PickSelectEntity<List<PickSelectEntity>>>>> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
            }
        });
    }

    //-----权限控制----------------------------------------------------------------
    public void getPermissionMenu(int websiteType) {
        String companyNo = SharedPreferenceUtil.getInstance().getSaveStringData(SharedPreferenceUtil.COMPANY_NO, "");

        if (!TextUtils.isEmpty(companyNo)) {
            getBottomPermission(companyNo, websiteType);
        }
    }

    //如果是自己获取菜单权限
    public void getBottomPermission(String websiteNo, int websiteType) {
        List<PermissionMenuEntity> data = DatalUtils.getInstance().getData(MkvUtils.CacheUserCenterMenu, CacheMarker.USER_CENTER_MENU + websiteNo, new TypeToken<List<PermissionMenuEntity>>() {
        });
        if (data != null) {
            permission_list.clear();
            permission_list.addAll(data);
            getOrderMenus(data);
        }
    }

    //企业订单全选
    public void getOrderMenus(List<PermissionMenuEntity> data) {

        mCheckMenus.clear();
        mCheckMenus.add(new PickFilterEntity<Object>("9", "员工审核"));
        mCheckMenus.add(new PickFilterEntity<Object>("10", "客户审核"));
        mCheckMenusLiveData.postValue(mCheckMenus);
    }
}
